$().ready(function() {
	validateRule();
    $("#topicTypes").val($("#topicTypesHidden").val());
    $("#itemBankType").val($("#itemBankTypeHidden").val());
    // $("#grade").val($("#gradeHidden").val());
    $("#state").val($("#stateHidden").val());
    $("#type").val($("#typeHidden").val());
    $("#resourceType").val($("#resoureTypesHidden").val());

    getVie($("#resoureTypesHidden").val());
    $("#resourceType").change(function () {
        $("#fileRoute").val("");
        $("#test").html("");
        var type = $(this).val();
        getVie(type)
    });

});
layui.use(['upload'], function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload',
        url: '/common/sysFile/upload',
        accept: 'file',
        // exts: 'docx|doc|xlsx|pdf|ppt',
        done: function (r) {
            $("#fileRoute").val(r.data);
            $("#test").html(subFileName(r.data));
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});
function getVie(type) {
    if (type != '') {
        if (type!=0&&type!='') {
            $("#resourse").show();
            $("#btnUpload").show()
        } else {
            $("#resourse").hide();
        }
    } else {
        $("#resourse").hide();
    }
}
$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
$(function () {
    $("#resourceType").change(function () {
        $("#enclosure").val("");
        $("#test").html("");
        var type = $(this).val();
        if (type!=0&&type!='') {
            $("#resourse").show();
            $("#btnUpload").show()
        } else {
            $("#resourse").hide();
        }
    });
});
//证书下拉框获取知识点数据
getKnowledges($("#profileIdHidden").val());
$("#profileId").change(function () {
    getKnowledges($(this).val())
});
function getKnowledges(cprofileId) {
    $("#kpointsId").empty();
    $("#know").html("");
    $.ajax({
        url: '/jzweb/itemBank/byKnowledges',
        data :{cprofileId:cprofileId} ,
        success: function (r) {
            var val =$("#kpointsIdHidden").val();
            if(r.data.length==0){
                $("#kpointsId").append('<option value="">暂无数据</option>');
            }else{
                $("#kpointsId").append('<option value="">请选择</option>');
                for(var p in r.data){
                    if(val!=r.data[p].id){
                        $("#kpointsId").append('<option value="'+r.data[p].id+'">'+r.data[p].name+'</option>');
                    }else{
                        $("#kpointsId").append('<option selected  value="'+r.data[p].id+'">'+r.data[p].name+'</option>');
                    }

                }
            }

        }
    })
}

//知识点选择获取等级数据
getGrades($("#kpointsIdHidden").val(),0);
$("#kpointsId").change(function () {
    getGrades($(this).val(),1)
});

function getGrades(knowId,type) {
    var seGradeList = $("#gradeHidden").val().split(",");
    $.ajax({
        url: '/jzweb/itemBank/byGrades',
        data :{knowId:knowId} ,
        success: function (r) {
            var gradeList = r.data.split(",");
            //选中的等级
            var list ='';
            if(r.data.length==0){
                list+='暂无数据';
            }else{
                for(var p in gradeList){
                    list+='<span><input name="grade" type="checkbox" value="'+gradeList[p]+'"/>'+gradeList[p]+'</span>';
                }
            }
            list+='</select>';
            $("#know").html(list);
            //渲染选中状态到页面,type为0进页面时才需要
            if(type==0){
                $("input[name='grade']").each(function () {
                    for(var i=0;i<seGradeList.length;i++){
                        if($(this).val()==seGradeList[i]){
                            $(this).attr("checked",true)
                        }
                    }
                });
            }
        }
    })
}

function update() {
    let flag=true;
    let topicTypes = $("#topicTypes").val();
    let options = $("#options").val();
    if(topicTypes == 0 || topicTypes == 1 || topicTypes == 2) {
        if(options == null && options == '' && options == undefined  && options == 'undefined' ){
            flag=false;
        }
    }
    if(flag){
        $.ajax({
            cache: true,
            type: "POST",
            url: "/jzweb/itemBank/update",
            data: $('#signupForm').serialize(),// 你的formid
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }

            }
        });
    }else {
        layer.alert('请输入答案选项');
    }
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
        rules : {
            kpointsId : {
                required : true
            },
            profileId : {
                required : true
            },
            itemBankType : {
                required : true
            },
            topicTypes : {
                required : true
            },
            topicContent : {
                required : true
            },
            topicAnalysis : {
                required : false
            },
            score : {
                required : true
            },
            grade : {
                required : true
            }
        },
        messages : {
            kpointsId : {
                required : icon + "请选择知识点"
            },
            profileId : {
                required : icon + "请选择证书概况"
            },
            itemBankType : {
                required : icon + "请选择题库类型"
            },
            topicTypes : {
                required : icon + "请选择题目类型"
            },
            topicContent : {
                required : icon + "请输入题目内容名"
            },
            topicAnalysis : {
                required : icon + "请输入题目解析"
            },
            score : {
                required : icon + "请输入分值"
            }
        }
	})
}